package com.hui.stock.security.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hui.stock.vo.resp.R;
import com.hui.stock.vo.resp.ResponseCode;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author: ymh
 * @date: 2025/1/13 11:31
 * @description:
 */
public class StockAuthenticationEntryPoint implements AuthenticationEntryPoint {
    /**
     * 匿名用户无权访问该处理器
     * @param request
     * @param response
     * @param e 拒绝时抛出的异常
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
        System.out.println(e.getMessage());
        //响应数据格式
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        //编码格式
        response.setCharacterEncoding("UTF-8");
        //响应
        response.getWriter().write(new ObjectMapper().writeValueAsString(R.error(ResponseCode.ANONMOUSE_NOT_PERMISSION)));
    }
}
